//
//  YSSearchArticleListView.m
//  Yueshi
//
//  Created by yueshi－mac1 on 16/2/22.
//  Copyright © 2016年 悦食中国. All rights reserved.
//

#import "YSSearchArticleListView.h"
#import "YSSearchListHeaderView.h"
#import "YSSearchArticleCell.h"
#import "YSReviewsCtrl.h"

@interface YSSearchArticleListView()<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic) UITableView *tableView;
@property (strong,nonatomic) NSMutableArray *itemArray;
@property (strong,nonatomic) YSSearchListHeaderView *headerView;
@property (assign,nonatomic) CGFloat allCellHeight;
@end

@implementation YSSearchArticleListView

-(NSMutableArray *)itemArray{
    if(!_itemArray){
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}
- (instancetype)initWithY:(CGFloat)y{
    self = [super init];
    if (self) {
        [self setFrame:[UIScreen mainScreen].bounds];
        self.headerView = [YSSearchListHeaderView headerView];
        self.headerView.hasGrayLine = y>0;
        [self initTableView];
        [self setBackgroundColor:[UIColor whiteColor]];
    }
    return self;
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]init];
    [self.tableView setFrame:CGRectMake(0, 0, self.width, self.height)];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.tableView setTableHeaderView:self.headerView];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 8, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    [self addSubview:self.tableView];
}

- (void)setMoreClick:(void (^)())moreClick{
    self.headerView.moreClick = moreClick;
}

- (void)setArray:(NSArray *)array{
    [_itemArray addObjectsFromArray:array];
    [self.tableView reloadData];
    
    _allCellHeight += CGRectGetHeight(self.headerView.frame);
    [self.tableView setTableHeaderView:self.headerView];
    [self.headerView setData:@"相关文章"];
    [self.headerView setCount:array.count];
    
    _allCellHeight += _itemArray.count*YSSearchArticleCellHeight;

    [self.tableView setHeight:_allCellHeight];
    [self setHeight:self.tableView.height];
}

#pragma - mark UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.itemArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YSSearchArticleCell *cell =  [YSSearchArticleCell cellWithTableView:tableView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setReview:self.itemArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return YSSearchArticleCellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YSReviews *reviews =  self.itemArray[indexPath.row];
    YSReviewsCtrl *ctrl = [YSReviewsCtrl initWithReviews:reviews];
    [self.superview.viewController.navigationController pushViewController:ctrl animated:YES];
}

@end
